package com.rykj.widget.bottomnavigation;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rykj.R;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;


/**
 * description:
 * <p/>
 * @author ycw Created by ycw on 2017/7/17.
 */
public class ItemView extends LinearLayout implements BottomObserver {

    private ImageView imageView;

    private TextView textView;

    public ItemView(Context context) {
        this(context, null);
    }

    public ItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initial(context);
    }

    private void initial(Context context) {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        setClipChildren(false);

        inflate(context, R.layout.nanavigation_item_view, this);

        imageView = (ImageView) findViewById(R.id.image_view);
        textView = (TextView) findViewById(R.id.text_view);
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public void setTextView(TextView textView) {
        this.textView = textView;
    }

    public void setText(CharSequence text) {
        textView.setText(text);
    }

    public void setImage(Drawable drawable) {
        imageView.setImageDrawable(drawable);
    }

    public void setImage(@DrawableRes int resId) {
        imageView.setImageResource(resId);
    }

    public CharSequence getTextViewString() {
        return textView.getText();
    }

    @Override
    public void update(BottomObserver observer) {
        boolean selected = observer.equals(ItemView.this);
        textView.setSelected(selected);
    }
}
